#include "wheel.h"
#include "epoll.h"
// 获得连接后更新连接在环形队列中位置
int receiveUidRequest(int uid) {
    // 找到连接在环形队列中旧有的位置
    int oldIndex = uidMap[uid];
    // 若有则将连接移除该slot的记录数组
    if(oldIndex != -1) {
        wheel[oldIndex][uid] = 0;
    }

    // 将连接移入当前索引的上一位置
    int newIndex = (currIdx - 1 + WHEEL_SIZE) % WHEEL_SIZE;
    wheel[newIndex][uid] = 1;
    uidMap[uid] = newIndex;
    
    return 0;
}

// 每一秒当前索引加一，所处的slot上的连接属于过期连接，将其关闭
int timeTick(int epfd) {
    currIdx = (currIdx + 1) % WHEEL_SIZE;

    for(int uid = 0; uid <= maxFd; ++uid) {
        if(wheel[currIdx][uid] == 1) {
            printf("netfd = %d timeout.\n", uid);
            close(uid);
            epollDel(epfd, uid);
            wheel[currIdx][uid] = 0;
            uidMap[uid] = -1;
        }
    }
    return 0;
}


